package test.hystrix.client;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

public class HelloCommand  extends HystrixCommand<String>{
	
	private String url;
	
	CloseableHttpClient httpClient;

	public HelloCommand(String url) {
		super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
		this.httpClient = HttpClients.createDefault();
		this.url = url;
	}

	@Override
	protected String run() throws Exception {
		try {
			HttpGet httpget =new HttpGet(url);
			HttpResponse response = httpClient.execute(httpget) ;
			return EntityUtils.toString(response.getEntity());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	@Override
	protected String getFallback() {
		return "time out error";
	}

}
